package _spring_test._tests;

import _spring_test.Inventor;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.expression.EvaluationContext;
import org.springframework.expression.Expression;
import org.springframework.expression.ExpressionParser;
import org.springframework.expression.spel.standard.SpelExpressionParser;
import org.springframework.expression.spel.support.StandardEvaluationContext;

import java.util.Date;
import java.util.GregorianCalendar;

/**
 * @Author: huangya
 * @Date: 2019/10/13
 * @Description:
 */
public class SpELTest222 {

    public static void main(String[] args) {
//        ExpressionParser parser1 = new SpelExpressionParser();
//        Expression exp1 = parser1.parseExpression("'Hello World'");
//        String message1 = (String) exp1.getValue();
//        System.out.println(message1);
//
//        ExpressionParser parser2 = new SpelExpressionParser();
//        Expression exp2 = parser2.parseExpression("'Hello World'.concat('!')");
//        String message2 = (String) exp2.getValue();
//        System.out.println(message2);
//
//
//        ExpressionParser parser3 = new SpelExpressionParser();
//        // invokes 'getBytes()'
//        Expression exp3 = parser3.parseExpression("'Hello World'.bytes");
//        byte[] bytes3 = (byte[]) exp3.getValue();
//        System.out.println(bytes3);
//        Expression exp32 = parser3.parseExpression("'Hello World'.bytes.length");
//        int length32 = (Integer) exp32.getValue();
//        System.out.println(length32); //11
//
//
//        ExpressionParser parser4 = new SpelExpressionParser();
//        Expression exp4 = parser4.parseExpression("new String('hello world').toUpperCase()");
//        String message4 = exp4.getValue(String.class);
//        System.out.println(message4); //HELLO WORLD
//
//
//        // Create and set a calendar
//        GregorianCalendar c = new GregorianCalendar();
//        c.set(1856, 7, 9);
//        // The constructor arguments are name, birthday, and nationality.
//        Inventor tesla = new Inventor("Nikola Tesla", c.getTime(), "Serbian");
//        ExpressionParser parser = new SpelExpressionParser();
//        Expression exp = parser.parseExpression("name");
//        String name = (String) exp.getValue(tesla);
//        System.out.println(name); // name == "Nikola Tesla"
//        exp = parser.parseExpression("name == 'Nikola Tesla'");
//        boolean result = exp.getValue(tesla, Boolean.class);
//        System.out.println(result); //true



        AnnotationConfigApplicationContext acac = new AnnotationConfigApplicationContext(Inventor.class);
        Inventor inventor = (Inventor) acac.getBean("inventor");
        ExpressionParser parser = new SpelExpressionParser();
        //解析表达式需要的上下文，解析时有一个默认的上下文
        EvaluationContext ctx = new StandardEvaluationContext();
        //在上下文中设置变量，变量名为user，内容为user对象
        ctx.setVariable("inventor", inventor);
//        ((StandardEvaluationContext)ctx).setRootObject(inventor);
        Expression exp = parser.parseExpression("T(java.lang.Math).abs(-2*(3+1)+4/2)==6 and " +
                "('CN'.equals(#inventor.getNationality()) || 1 eq 2) ? 8 : 9");
        Integer value = (Integer) exp.getValue(ctx);
//        Expression exp = parser.parseExpression("'a'");
//        String value = (String) exp.getValue(ctx);
        System.out.println(value);
    }
}
